package com.scala.func

/**
 * Created by Administrator on 2018/1/29/029.
 */
object FunctionDemo {

  def main(args: Array[String]) {

    //函数赋值给变量
    def sayHello(name:String)={println("hello,"+name)}
    val ss = sayHello _
    ss("zhenghui")

    //匿名函数
    val sayHelloFunc = (name:String) => println("Hello ," + name)
    sayHelloFunc("eric")

    //高阶函数 String => Unit表示函数
    val sayHelloFun = (name:String,age:Int) => println("Hello ," + name+",I'm "+ age +" years old")
    def getGreetingFunc(func:(String,Int) => Unit,name:String,age:Int): Unit ={
      func(name,age )
    }
    getGreetingFunc(sayHelloFun,"eric",31)
    //高阶函数的另外一个功能
    def getGreetingFunR(msg:String)=(name:String) => println(msg + "," + name)
    val greetingFunc = getGreetingFunR("hello")
    greetingFunc("leo")
    val greetingFunc2 = getGreetingFunR("Hi")
    greetingFunc2("leo")





    //下划线
    def greeting(func:(String)=> Unit,name:String){func(name)}
    greeting((name:String)=> println("hello ,"+name),"leo")
    greeting((name)=> println("hello ,"+name),"leo")
    greeting(name=> println("hello ,"+name),"leo")
    Array(1,2,3,4,5).map(2 * _)

     def triple(func:(Int) => Int) = { func(3)}
    println(triple(3 *_))
    println(triple(6 * _))
  }
}
